﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace CoreLims.Models.CoreLims
{
    public static class RetentionConfigExtension
    {
        public static List<RetentionConfig> getRetentionConfigList(this RetentionConfig rc, CoreLimsContext dbContext) => dbContext.RetentionConfig.ToList();

        public static RetentionConfig getRetentionConfig(this RetentionConfig rc, CoreLimsContext dbContext, string Id) =>
            dbContext.RetentionConfig.FirstOrDefault(s => s.Id == Id);

        public static string Add(this RetentionConfig rc, CoreLimsContext dbContext)
        {
            rc.Id = Guid.NewGuid().ToString();
            dbContext.Add(rc);
            dbContext.SaveChanges();
            return "留样配置添加成功！";
        }

        public static string Update(this RetentionConfig rc, CoreLimsContext dbContext)
        {
            RetentionConfig retention = SAMPLE.RetentionConfig.getRetentionConfig(dbContext, rc.Id);
            retention.Copy(rc);
            dbContext.Update(retention);
            dbContext.SaveChanges();
            return "留样配置更新成功！";
        }

        public static string Delete(this RetentionConfig rc, CoreLimsContext dbContext, string Id)
        {
            RetentionConfig rec = rc.getRetentionConfig(dbContext, Id);
            dbContext.Remove(rec);
            dbContext.SaveChanges();
            return "留样配置删除成功！";
        }

        public static string UpdatePosition(this RetentionConfig rc, CoreLimsContext dbContext, string Id, string position)
        {
            RetentionConfig rec = rc.getRetentionConfig(dbContext, Id);
            rec.Position = position;

            dbContext.Update(rec);
            dbContext.SaveChanges();
            return "存储位置更新成功！";
        }

        public static string UpdateStorageConditions(this RetentionConfig rc, CoreLimsContext dbContext, string Id, string storageConditions)
        {
            RetentionConfig rec = rc.getRetentionConfig(dbContext, Id);
            rec.StorageConditions = storageConditions;

            dbContext.Update(rec);
            dbContext.SaveChanges();
            return "存储条件更新成功！";
        }

        /// <summary>
        /// 复制记录内容，ID不复制
        /// </summary>
        /// <param name="supplier"></param>
        /// <param name="other"></param>
        /// <returns></returns>
        private static RetentionConfig Copy(this RetentionConfig rc, RetentionConfig other)
        {
            rc.Name = other.Name;
            rc.Position = other.Position;
            rc.StorageConditions = other.StorageConditions;
            rc.Container = other.Container;
            rc.Quantity = other.Quantity;
            rc.Unit = other.Unit;
            rc.ContainerNumber = other.ContainerNumber;
            rc.Duration = other.Duration;
            rc.DurationUnit = other.DurationUnit;
            rc.Remind = other.Remind;

            return rc;
        }

    }
}
